//
//  ThemeBarStylePicker.swift
//  HeadlineNews
//
//  Created by han on 2019/7/26.
//  Copyright © 2019 han. All rights reserved.
//

import UIKit

#if os(tvOS)

final class ThemeBarStylePicker: ThemePicker {}

#else

final class ThemeBarStylePicker: ThemePicker {
    convenience init(keyPath: String) {
        self.init(v: { ThemeBarStylePicker.getStyle(stringStyle: ThemeManager.string(for: keyPath) ?? "") })
    }
    
    convenience init(keyPath: String, map: @escaping (Any?) -> UIBarStyle) {
        self.init(v: { map(ThemeManager.value(for: keyPath))})
    }
    
    convenience init(styles: UIBarStyle...) {
        self.init(v: { ThemeManager.element(for: styles) })
    }
}

extension ThemeBarStylePicker {
    class func getStyle(stringStyle: String) -> UIBarStyle{
        switch stringStyle.lowercased() {
        case "default": return .default
        case "black": return .black
        default: return .default
        }
    }
}

extension ThemeBarStylePicker: ExpressibleByArrayLiteral {
    convenience init(arrayLiteral elements: UIStatusBarStyle...) {
        self.init(v: { ThemeManager.element(for: elements) })
    }
}

extension ThemeBarStylePicker: ExpressibleByStringLiteral {
    convenience init(stringLiteral value: String) {
        self.init(keyPath: value)
    }
    convenience init(extendedGraphemeClusterLiteral value: String) {
        self.init(keyPath: value)
    }
}

extension ThemeBarStylePicker: ExpressibleByUnicodeScalarLiteral {
    convenience init(unicodeScalarLiteral value: String) {
        self.init(keyPath: value)
    }
}

extension ThemeBarStylePicker {
    class func pickerWithKeyPath(_ keyPath: String) -> ThemeBarStylePicker{
        return ThemeBarStylePicker(keyPath: keyPath)
    }
    
    class func pickerWithKeyPath(_ keyPath: String, map: @escaping (Any?) -> UIBarStyle) -> ThemeBarStylePicker {
        return ThemeBarStylePicker(v: { map(ThemeManager.value(for: keyPath)) })
    }
    
    class func pickerWithStyles(_ styles: [UIBarStyle]) -> ThemeBarStylePicker {
        return ThemeBarStylePicker(v: { ThemeManager.element(for: styles) })
    }
    
    class func pickerWithStyles(_ styles: [String]) -> ThemeBarStylePicker {
        return ThemeBarStylePicker(v: { ThemeManager.element(for: styles.map(getStyle))})
    }
}

#endif
